<?php

namespace api\modules\app\models;

use Yii;

/**
 * This is the model class for table "unit_history".
 *
 * @property integer $id
 * @property string $unit_code
 * @property integer $user_id
 * @property string $user_name
 * @property integer $state
 * @property string $type1
 * @property string $type2
 * @property string $event_id
 * @property integer $history_time
 * @property integer $create_time
 */
class UnitHistory extends \api\modules\app\models\BaseModel
{
    const TYPE1_INFO = 1; //信息更新
    const TYPE1_EVENT = 2; //异常上报
    const TYPE1_UNIT = 3; //部件维护
    const TYPE1_SCAN = 4; //扫码

    const TYPE2_INFO_CREATE = 101; //创建
    const TYPE2_INFO_UPDATE = 102; //更新
    const TYPE2_EVENT_REPORT = 201; //案件上报
    const TYPE2_UNIT_MAINTAIN = 301; //设备维护
    const TYPE2_UNIT_INSPECT = 302; //巡检
    const TYPE2_UNIT_COMPLETE = 303; //维修完毕
    const TYPE2_SCAN_FROM_WX = 401; //微信扫码
    const TYPE2_SCAN_FROM_ZW = 402; //政务微信扫码
    const TYPE2_SCAN_FROM_OTHER = 4099;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'unit_history';
    }

    /**
     * @return \yii\db\Connection the database connection used by this AR class.
     */
    public static function getDb()
    {
        return Yii::$app->get('test_3');
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'state', 'history_time', 'create_time', 'type1', 'type2'], 'integer'],
            [['unit_code', 'user_name'], 'string', 'max' => 50],
            [['event_id'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'unit_code' => 'Unit Code',
            'user_id' => 'User ID',
            'user_name' => 'User Name',
            'state' => 'State',
            'type1' => 'Type1',
            'type2' => 'Type2',
            'event_id' => 'Event ID',
            'history_time' => 'History Time',
            'create_time' => 'Create Time',
        ];
    }

    public static function getType1List() {
        return [
            self::TYPE1_INFO => '信息更新',
            self::TYPE1_EVENT => '异常上报',
            self::TYPE1_UNIT => '部件维护',
            self::TYPE1_SCAN => '扫码',
        ];
    }

    public static function getType1ByKey($key) {
        $result = '';
        $list = self::getType1List();
        if (array_key_exists($key, $list)) {
            $result = $list[$key];
        }

        return $result;
    }

    public static function getType2List() {
        return [
            self::TYPE2_INFO_CREATE => '创建',
            self::TYPE2_INFO_UPDATE => '更新',
            self::TYPE2_EVENT_REPORT => '案件上报',
            self::TYPE2_UNIT_MAINTAIN => '设备维护',
            self::TYPE2_UNIT_INSPECT => '巡检',
            self::TYPE2_UNIT_COMPLETE => '维修完毕',
            self::TYPE2_SCAN_FROM_WX => '微信扫码',
            self::TYPE2_SCAN_FROM_ZW => '政务微信扫码',
            self::TYPE2_SCAN_FROM_OTHER => '其他来源扫码',
        ];
    }

    public static function getType2ByKey($key) {
        $result = '';
        $list = self::getType2List();
        if (array_key_exists($key, $list)) {
            $result = $list[$key];
        }

        return $result;
    }

    public static function getTypeMap() {
        return [
            self::TYPE1_INFO => [self::TYPE2_INFO_CREATE, self::TYPE2_INFO_UPDATE],
            self::TYPE1_EVENT => [self::TYPE2_EVENT_REPORT],
            self::TYPE1_UNIT => [self::TYPE2_UNIT_MAINTAIN, self::TYPE2_UNIT_INSPECT,self::TYPE2_UNIT_COMPLETE],
            self::TYPE1_SCAN => [self::TYPE2_SCAN_FROM_WX, self::TYPE2_SCAN_FROM_ZW, self::TYPE2_SCAN_FROM_OTHER]
        ];
    }

    /**
     * 通过小类找大类
     * @param $type2
     */
    public static function getType1ByType2($type2)
    {
        //检查type2是否存在，不存在返回空
        $type2Str = self::getType2ByKey($type2);
        if (empty($type2Str)) {
            return '';
        }

        $result = '';
        $map = self::getTypeMap();
        foreach ($map as $k => $v) {
            if (in_array($type2, $v)) {
                $result = $k;
            }
        }

        return $result;
    }

}

